package cn.tx.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.tx.dao.EmpDao;
import cn.tx.model.Emp;
import cn.tx.query.EmpQuery;

public class EmpDaoImpl extends BaseDaoImpl<Emp, EmpQuery> implements EmpDao  {

	@Override
	public String createHql(EmpQuery q) {
		String hql = "from Emp e where 1=1 ";
		String condition = this.createConditionHql(q);
		hql = hql + condition +" order by e.empId desc";
		return hql;
	}

	@Override
	public String createHqlCount(EmpQuery q) {
		String hql = "select count(e) from Emp e where 1=1 ";
		//StringUtils.isNotBlank判断当前的字符串是不是空串和null
		String condition = this.createConditionHql(q);
		hql = hql + condition;
		return hql;
	}

	@Override
	public String createConditionHql(EmpQuery q) {
		String hql = "";
		if(StringUtils.isNotBlank(q.getUsername())){
			hql = hql + "and e.username like :username ";
		}
		if(StringUtils.isNotBlank(q.getName())){
			hql = hql + "and e.name like :name ";
		}
		if(StringUtils.isNotBlank(q.getTel())){
			hql = hql + "and e.tel like :tel ";
		}
		if(StringUtils.isNotBlank(q.getEmail())){
			hql = hql + "and e.email like :email ";
		}
		if(q.getGender() != null){
			hql = hql + "and e.gender = :gender ";
		}
		if(q.getStartBirth() != null){
			hql = hql + "and e.birthday > :startBirth ";
		}
		if(q.getEndBirth() != null){
			hql = hql + "and e.birthday < :endBirth ";
		}
		if(q.getDepId() != null){
			hql = hql + "and e.dep.depId = :depId ";
		}
		return hql;
	}


	@Override
	public Emp getEmpByUsername(String username) {
		List list = this.getHibernateTemplate().find("from Emp e where e.username = ?", username);
		if(list.size() > 0){
			return (Emp) list.get(0);
		}else{ 
			return null;
		}
	}

	@Override
	public Emp login(String username, String password) {
		List list = this.getHibernateTemplate().find("from Emp e where e.username = ? and e.password = ?", new Object[]{username, password});
		if(list.size() > 0){
			return (Emp) list.get(0);
		}else{ 
			return null;
		}
		
	}



	

}
